package net.feling.mcloud.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLDecoder;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;

import android.util.Log;
import org.apache.http.protocol.HTTP;

public class HttpFetcher {

	private static final String TAG = "HttpFetcher";

	public HttpFetcher() {
	}

	/**
	 * 将一个输入流转化为字符串
	 */
	public static String getStreamString(InputStream tInputStream) {
		if (tInputStream != null) {
			try {
				BufferedReader tBufferedReader = new BufferedReader(
						new InputStreamReader(tInputStream));
				StringBuffer tStringBuffer = new StringBuffer();
				String sTempOneLine = new String("");
				while ((sTempOneLine = tBufferedReader.readLine()) != null) {
					tStringBuffer.append(sTempOneLine);
				}
				return tStringBuffer.toString();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 
	 * @param url
	 * @param postData
	 * @return null if net err
	 */
	public String postString(String url, List<NameValuePair> postData) {

		InputStream in = this.postStream(url, postData);
		String result = getStreamString(in);

		if (result != null) {
			Log.i(TAG, result);
		} else {
			Log.i(TAG, TAG + "null");
		}
		return result;
	}

	public InputStream postStream(String url,
			List<? extends NameValuePair> postData) {
		InputStream inputstream = null;
		HttpClient httpclient = new DefaultHttpClient();
		//请求超时
		httpclient.getParams().setParameter(
				CoreConnectionPNames.CONNECTION_TIMEOUT, 3000);
		//读取超时
		httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,
				3000);
		HttpPost httpPost = new HttpPost(url);
		try {
			Log.i(TAG, url);
			Log.i(TAG, URLDecoder.decode(postData.toString(),"utf8"));
			httpPost.setEntity(new UrlEncodedFormEntity(postData));
			HttpResponse response = httpclient.execute(httpPost);
			if (response.getStatusLine().getStatusCode() == 200) {
				inputstream = response.getEntity().getContent();
			}
		} catch (Exception e) {
			return null;
		}
		return inputstream;
	}

	/**
	 * get 方法，我好像没写带参数的，不能向服务端传参数
	 * @param url
	 * @return
	 */
	public InputStream getStream(String url) {
		Log.i(TAG, url);
		InputStream inputstream = null;
		HttpClient httpclient = new DefaultHttpClient();
		httpclient.getParams().setParameter(
				CoreConnectionPNames.CONNECTION_TIMEOUT, 3000);
		httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,
				3000);
		HttpGet httpGet = new HttpGet(url);
		try {
			HttpResponse response = httpclient.execute(httpGet);
			if (response.getStatusLine().getStatusCode() == 200) {
				inputstream = response.getEntity().getContent();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return inputstream;
	}

	/**
	 * get 方法，我好像没写带参数的，不能向服务端传参数
	 * @param url
	 * @return null if net err
	 */
	public String getString(String url) {
		String result = "";// 初始化时，不能是null，null会被加到结果字符串中
		BufferedReader reader = null;
		String line = null;
		InputStream in = this.getStream(url);
		if (in == null) {
			result = null;
		} else {
			try {
				reader = new BufferedReader(new InputStreamReader(in));
				while ((line = reader.readLine()) != null) {
					result = result + line;
				}
			} catch (Exception e) {
				e.printStackTrace();
				result = null;
			} finally {
				if (in != null && reader != null) {
					try {
						in.close();
						reader.close();
					} catch (IOException e) {
						e.printStackTrace();
						result = null;
					}
				}
			}
		}
		if (result != null) {
			Log.i(TAG, result);
		} else {
			Log.i(TAG, TAG + "null");
		}
		return result;
	}
}
